# -*- coding: utf-8 -*-
class SysMsgController < ApplicationController
  def sysMsgList
    
  end

  #得到所有的消息
  def getAllSysMsgs
    start = params[:start]
    limit = params[:limit]
    sysMsgsAll = SysMsg.employee_like session[:company_id] , session[:employee_id]
    if start.nil?
      sysMsgs = sysMsgsAll
    else
      sysMsgs = sysMsgsAll.paginate start,limit
    end
    jsonStr="{totalProperty:"+sysMsgsAll.count.to_s+",root:"+sysMsgs.to_json()+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #得到所有的未读消息条数
  def getUnreadSysMsgsSize    
    sysMsgsAll = SysMsg.employee_like(session[:company_id] , session[:employee_id])
    sysMsgsAll = sysMsgsAll.unread
    jsonStr = sysMsgsAll.count.to_s
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #根据Id得到消息
  def getMsgById
    sysMsg = SysMsg.find(params[:id])
    if sysMsg.is_new == '未读'
      sysMsg.is_new = '已读'
      sysMsg.save!
    end
    jsonStr = "{success:true,info:'#{sysMsg.content}'}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def delOldSysMsg
    #先删除所有的2天以前的数据。
    SysMsg.delete_all("created_at < '#{2.day.ago}'")
  end
end
